<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Verify Attendance</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="./EldercareMasterPage/_finalstyle.css" rel="stylesheet" />
	<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="./css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
	<link rel="stylesheet" type="text/css" href="./jquery/easyui.css">
	<link rel="stylesheet" type="text/css" href="./jquery/icon.css">
</head>
<?php
    define('CONFIG_FILE',true);
    include './config.php';
    define('DB_FILE',true);
    
    session_start();
    if (empty($_SESSION["username"])||empty($_SESSION["userType"])) {
        Header("Location: ./Index.php");
        exit();
    }else{
        $userType = $_SESSION["userType"];
    }
    
    if ($userType == "Therapy Assistant") {
        $url="./Index.php";
		include_once './Tool/Common/Redirect.php';
		exit();
    }
    
    // set timeout period in seconds
    $inactive = 7200;
    // check to see if $_SESSION['timeout'] is set
    if (isset($_SESSION["timeout"])) {
        $session_life = time() - $_SESSION["timeout"];
        if ($session_life > $inactive) {
            session_destroy();
            Header("Location: Timeout.php");
        }
    }
    $_SESSION["timeout"] = time();
?>
    <body>
		<div class="banner">
			</div>
			<div id="main">
				<div class="left-wrapper">
					<div class="logo">
						<img src="images/eldercare.gif" alt="logo" width="240px" />
					</div>
					<div class="menu-wrapper">
						<div id="cssmenu">        
								<?php
								if ($userType == "Therapy Assistant") {
									include_once('./Sidebar/_SidebarTA.php');
								} else if ($userType == "Centre Manager") {
									include_once('./Sidebar/_SidebarCM.php');
								} else {
									include_once('./Sidebar/_SidebarSA.php');
								}
								?>                     
						</div> 
						<div class="clear"></div></div>
				</div>
                <div id="content">
                    <div class="content2">
                        <b style="float:right; margin: 0px 0 0 0">  
                            <?php
                                if(!empty($_SESSION["centreName"])){
                                    $Username = $_SESSION["username"];
                                    $centreName = $_SESSION["centreName"];
                                    echo "Welcome, " . $Username . " ! " . $centreName . " Branch";
                                }else{
                                    $Username = $_SESSION["username"];
                                    echo "Welcome, " . $Username;
                                }
                            ?>
							<input type="submit" class="btn btn-danger" value="Logout" onclick="parent.location = 'logout.php'">
						</b>
                        </p>
						<br>
						<div style="float:center">
							<br>
                            <h3>Verify Attendance</h3>
                            <br>
                            <table id="dg" title="Verify Attendance" class="easyui-datagrid" style="width:auto;height:390px; "
                                   url="./CRUDVerify/get_verify.php"
                                   toolbar="#toolbar" pagination="true"
                                   rownumbers="true" fitColumns="true" singleSelect="false">
                                <thead>
                                    <tr>
                                    <th field="NRIC" width="50" sortable="true" >NRIC</th>
                                    <th field="CustomerName" width="50" sortable="true" >Name</th>
                                    <?php
	                                    if ($userType == "System Admin") {
	                                        echo "<th field='CentreName' width='50' sortable=\"true\">Centre Name</th>";
	                                    }
                                    ?>
                                    <th field="Date" width="50" sortable="true">Date</th>
                                    <th field="TimeIn" width="50" sortable="true">Time In</th>
                                    <th field="TimeOut" width="50" sortable="true"> Time Out</th>
                                    <th field="Status" width="50" sortable="true">Status</th>
                            <tbody style="display:none">
                            <th field="AttendanceRecordID" width="auto">Attendance Record ID</th></tbody>
                            </tr>
                            </thead>
                        </table>
                        <div id="toolbar">
                            <a href='#' class='easyui-linkbutton' iconCls='icon-ok' plain='true' onclick='verifyAttendance()'>Verify Attendance</a>
                            <a href='#' class='easyui-linkbutton' iconCls='icon-ok' plain='true' onclick='disp_confirm()()'>Verify All Attendance</a>  
                            <a href='#' class='easyui-linkbutton' iconCls='icon-remove' plain='true' onclick='removeAttendance()'>Remove Attendance</a>  
                            <input id="searchid" name="searchid" type="text" style="border-radius:25px">  
                            <a href="#" class="easyui-linkbutton" iconCls="icon-search" onClick="getAttendance()">Search</a>
                            <a href="#" class="easyui-linkbutton" onClick="findAll()">All</a>
                        </div>
                    </div>
                        <p></p>
                    <div class="clear"></div>
					</div>
				</div>			
			</div>
		<div id="footer">&copy; 2013 MP22 IIT-IT Temasek Polytechnic</div>
	</body>
<script type="text/javascript" src="./jquery/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="./jquery/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="./jquery/jquery.easyui.min.js"></script>
<script type="text/javascript">
    var url;
    function removeAttendance() {
        var rows = $('#dg').datagrid('getSelections');
        if (rows) {
            $.messager.confirm('Confirm Delete Attendance', 'Are you sure you want to remove this attendance?', function(r) {
                if (r){
                	for (var i = 0; i < rows.length; i++){
	                    var row = rows[i];
	                    //validate AttendanceRecordID
	                    var check = 0;
	                    $.ajax({
	                        async : false,
	                        type : "POST",
	                        url : "./CRUDVerify/validateRemove.php", //the page/method you wanna validate
	                        data: {
	                            AttendanceRecordID: row.AttendanceRecordID
	                        },
	                        success : function(data) {
	                            check=data;
	                        }
	                    });
	                    if(check==0){
	                        $.messager.show({
	                            title: 'Error',
	                            msg: 'Invalid Attendance Record ID to remove.',
								showType:'fade',
								style:{
									right:'',
									bottom:''
								}
	                        });
	                    }else{
	                        $.post('./CRUDVerify/remove_verify.php?', {AttendanceRecordID: row.AttendanceRecordID}, function(result) {
	                            if (result.success) {
	                                $('#dg').datagrid('reload');	// reload the user data
	                            } else {
	                                $.messager.show({// show error message
	                                    title: 'Error',
	                                    msg: result.msg,
	                                    showType:'fade',
	                                    style:{
	                                        right:'',
	                                        bottom:''
	                                    }
	                                });
	                            }
	                        }, 'json');   
	                    }
	                }
                }
            });
        }
    }

    function verifyAttendance() {
        var rows = $('#dg').datagrid('getSelections');
        if (rows) {
            $.messager.confirm('Confirm Verify Attendance', 'Are you sure you want to verify this attendance?', function(r) {
                if (r){
                    for (var i = 0; i < rows.length; i++){
	                    var row = rows[i];
	                    $.post('./CRUDVerify/verify.php?', {AttendanceRecordID: row.AttendanceRecordID}, function(result) {
	                        if (result.success) {
	                        	$('#dg').datagrid('reload');	// reload the user data
	                        }else{
	                            $.messager.show({// show error message
	                                title: 'Error',
	                                msg: result.msg
	                            });
	                        }
	                    }, 'json');
	                }
                }
            });
        }
    }

    function getAttendance() {
        //validate search input    
        var check = 0;
        $.ajax({
            async : false,
            type : "POST",
            url : "./CRUDVerify/validateSearch.php", //the page/method you wanna validate
            data: "searchid=" + $("#searchid").val(),
            success : function(data) {
                check=data;
            }
        });
        if(check==0){
            $.messager.show({
                title: 'Error',
                msg: 'Invalid Search Input. Search Input cannot contain any special characters except bracket, dashes, colons and single quatation marks.',
				showType:'fade',
				style:{
					right:'',
					bottom:''
				}
            });
        }else{
            $('#dg').datagrid('load',{
                searchid: $('#searchid').val()
            });
        }
    }
    function findAll() {
        $('#dg').datagrid('load',{
        });
    }

    function disp_confirm(){
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $.messager.confirm('Confirm Verify Attendance', 'Are you sure you want to verify all attendance?', function(r) {
                if (r){
                    $.get('./CRUDVerify/update_verify.php');
                    $('#dg').datagrid('reload');
                }
            });
        }
    }
</script>			
</html>